package com.safewind.util;

import java.text.DecimalFormat;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;

public class StringUtil {

	private static String regEx = "[\u4e00-\u9fa5]";
	private static Pattern pat = Pattern.compile(regEx);

	public static String toMd5(String str) {
		return Hashing.md5().hashString(str, Charsets.UTF_8).toString();
	}

	public static String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^(1[3|4|5|7|8][0-9])\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	public static boolean isBlank(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		}
		return false;
	}

	public static boolean isNotBlank(String str) {
		if (str != null && !"".equals(str.trim())) {
			return true;
		}
		return false;
	}

	public static boolean isContainsChinese(String str) {
		Matcher matcher = pat.matcher(str);
		boolean flag = false;
		if (matcher.find()) {
			flag = true;
		}
		return flag;
	}

	public static String getKeyByValue(String value, Map<String, String> map) {
		String str = null;
		for (String key : map.keySet()) {
			String tvalue = map.get(key);
			if (value.equals(tvalue)) {
				str = key;
				break;
			}
		}
		return str;
	}

	public static boolean isContains(String str1, String str2) {
		Pattern p = Pattern.compile(str2);
		Matcher m = p.matcher(str1);
		while (m.find()) {
			return true;
		}
		return false;
	}

	public static String doubleToString(double d) {
		DecimalFormat df = new DecimalFormat("######0.0");
		return df.format(d);
	}

	/**
	 * 采用默认方式
	 * 
	 * @param content
	 * @param sendNumber
	 * @return
	 */
	public static Double getCostDefault(String content, int sendNumber) {
		int max = 160;
		if (StringUtil.isContainsChinese(content)) {
			max = 70;
		}
		int num = ((content.trim().length() % max) == 0 ? 0 : 1)
				+ (content.trim().length() / max);
		return num * sendNumber * ConstantUtil.VALUE_OF_ONE_MESSAGE;
	}

	/**
	 * 56计算方式
	 * 
	 * @param content
	 * @param sendNumber
	 * @return
	 */
	public static int getCost(String content, int sendNumber) {
		int length = Integer.parseInt(PropertiesUtil
				.getValue("OneMessageLength"));
		int num = ((content.length() % length) == 0 ? 0 : 1)
				+ (content.length() / length);
		return num * sendNumber;
	}

	/**
	 * 
	 * @param reply
	 * @return
	 */
	public static String analysisReply(int reply) {
		System.out.println("短信接口返回结果:" + reply);
		String answer = "";
		switch (reply) {
		case 1:
			answer = "成功";
			break;
		case 0:
			answer = "余额不足~";
			break;
		case -6:
			answer = "信息包含敏感内容~";
			break;
		case -7:
		case -8:
			answer = "网络故障~";
			break;
		default:
			answer = "系统故障，抢修中~";
			break;
		}
		return answer;
	}
}
